package validate

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
)

type LoginForm struct {
	Username string `json:"username,omitempty" validate:"username"`
	Password string `json:"password,omitempty" validate:"password"`
}

type SingupEmailExistForm struct {
	Email string `json:"email,omitempty" validate:"email"`
}

type RegisterForm struct {
	Email           string `json:"email,omitempty" validate:"email"`
	Password        string `json:"password,omitempty" validate:"password"`
	PasswordConfirm string `json:"password_confirm,omitempty" validate:"password_confirm"`
	Code            string `json:"code,omitempty" validate:"code"`
}

func ValidateLogin(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"username": []string{"required"},
		"password": []string{"required"},
	}

	message := govalidator.MapData{
		"username": []string{"required:Username is required"},
		"password": []string{"required:Password is required"},
	}

	return validate(data, rules, message)
}

func ValidateEmailExist(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"email": []string{"required", "email", "min:4", "max:50"},
	}

	messages := govalidator.MapData{
		"email": []string{
			"required:Email 为必填项",
			"min:Email 长度需大于 4",
			"max:Email 长度需小于 30",
			"email:Email 格式不正确，请提供有效的邮箱地址",
		},
	}

	return validate(data, rules, messages)
}

func ValidateRegitser(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"email":            []string{"required", "email", "not_exists:users,email"},
		"password":         []string{"required"},
		"password_confirm": []string{"required"},
		"code":             []string{"required", "len:6"},
	}

	message := govalidator.MapData{
		"email":            []string{"required:email is required", "email:Email must be a valid email"},
		"password":         []string{"required:Password is required"},
		"password_confirm": []string{"required:Password Confirm is required"},
		"code":             []string{"required:Code is required", "len:Code must be 6 digits"},
	}

	errs := validate(data, rules, message)
	_data := data.(*RegisterForm)
	errs = ValidatePasswordConfirm(_data.Password, _data.PasswordConfirm, errs)
	errs = ValidateVerifyCode(_data.Email, _data.Code, errs)

	return errs
}
